const express = require('express')
const multer  = require('multer')
const cors = require('cors');
const {join} = require("path");
const {mkdirSync} = require("fs");

const app = express()
app.use(cors());

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        try {
            mkdirSync(join(process.cwd() , '/my-uploads'))
        } catch (e) {}
        cb(null, join(process.cwd(), '/my-uploads'))
    },
    filename: function (req, file, cb) {
        const filename = Buffer.from(file.originalname, "latin1").toString(
            "utf8"
        );
        cb(null, filename)
    }
})

const upload = multer({ storage })

app.post('/aaa', upload.single('file'), function (req, res, next) {
    console.log('req.file', req.file);
    console.log('req.body', req.body);
    res.send('上传成功')
})
app.post('/bbb', upload.array('bbb', 2), function (req, res, next) {

    console.log('req.body', req.body);
    res.send('上传成功')
})

app.post('/ccc', upload.any(), function (req, res, next) {
    console.log(req.files)
    console.log('req.body', req.body);
    res.send('上传成功')
})
app.listen(3333, () => {
    console.log('开启成功')
});
